CXX           = g++
CFLAGS        = -pipe -Wall -g -W $(DEFINES)
CXXFLAGS      = -pipe -Wall -g -DDEBUG $(DEFINES)
INCPATH       = -I/usr/include/opencv -I./src/
LINK          = g++
LFLAGS        = -Wl,-O1
#opencv version 2.3
#LIBS          = -L/usr/lib -l:libopencv_ml.so.2.3 -l:libopencv_highgui.so.2.3 -l:libopencv_core.so.2.3 -l:libopencv_imgproc.so.2.3
#opencv version 2.1
LIBS          = -L/usr/lib -lml -lhighgui -lcxcore -lcv
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p


###################################################
# LM
###################################################

SRCS =	src/Main.cpp \
		src/test.cpp \
		src/RecognitionTester.cpp \
		src/common.cpp \
		src/TrackCatcher.cpp \
		src/algs/Algorithms.cpp \
		src/algs/TrainAlgs.cpp \
		src/model/HMM.cpp \
		src/model/Track.cpp \
		src/model/Point.cpp \
		src/io/HMMBuilder.cpp \
		src/io/BaseSaver.cpp \
		src/ui/LaserFinder.cpp \
		src/ui/Calibrator.cpp \
		src/ui/CornerCalibrator.cpp \
		src/ui/LaserManualCalibrator.cpp \
		src/ui/LaserAutoCalibrator.cpp \
		src/parsing/Lexer.cpp \
		src/parsing/CommandLineParser.cpp \
		src/dispatcher/LaserEvent.cpp \
		src/dispatcher/LaserDispatcher.cpp \
		src/database/TrackBase.cpp \
		src/calculator/Calculator.cpp \
		src/Learner.cpp \
		src/crisscross/CrissCross.cpp \
		src/crisscross/CrissCrossListener.cpp
		

OBJS0 = $(SRCS:%.cpp=%.o)
OBJS = $(OBJS0:src/%=bin/%)

HDRS =	src/algs/Algorithms.h \
		src/algs/TrainAlgs.h \
		src/model/HMM.h \
		src/model/Track.h \
		src/model/Point.h \
		src/io/HMMBuilder.h \
		src/io/BaseSaver.h \
		src/ui/LaserFinder.h \
		src/ui/Calibrator.h \
		src/ui/CornerCalibrator.h \
		src/ui/LaserManualCalibrator.h \
		src/ui/LaserAutoCalibrator.h \
		src/parsing/Lexer.h \
		src/parsing/CommandLineParser.h \
		src/model/MappedRecognizer.h \
		src/model/MappedRecognizer.impl.h

all : bin/lm

bin/lm : $(OBJS)
	$(CXX) $(INCPATH) $(CXXFLAGS) $(OBJS) $(LINKFLAGS) $(LIBS) -o bin/lm

bin/Main.o :  $(HDRS) src/Main.cpp src/common.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/Main.cpp -o bin/Main.o

bin/test.o : src/test.cpp src/test.h src/common.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/test.cpp -o bin/test.o

bin/RecognitionTester.o : src/RecognitionTester.cpp src/RecognitionTester.h src/common.h \
			src/model/MappedRecognizer.h src/model/MappedRecognizer.impl.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/RecognitionTester.cpp -o bin/RecognitionTester.o

bin/common.o : src/common.cpp src/common.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/common.cpp -o bin/common.o

bin/TrackCatcher.o : src/TrackCatcher.cpp src/TrackCatcher.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/TrackCatcher.cpp -o bin/TrackCatcher.o

bin/algs/Algorithms.o : src/algs/Algorithms.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/algs/Algorithms.cpp -o bin/algs/Algorithms.o

bin/algs/TrainAlgs.o : src/algs/TrainAlgs.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/algs/TrainAlgs.cpp -o bin/algs/TrainAlgs.o

bin/model/HMM.o : src/model/HMM.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/model/HMM.cpp -o bin/model/HMM.o

bin/model/Track.o : src/model/Track.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/model/Track.cpp -o bin/model/Track.o

bin/model/Point.o : src/model/Point.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/model/Point.cpp -o bin/model/Point.o

bin/io/HMMBuilder.o : src/io/HMMBuilder.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/io/HMMBuilder.cpp -o bin/io/HMMBuilder.o

bin/io/BaseSaver.o : src/io/BaseSaver.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/io/BaseSaver.cpp -o bin/io/BaseSaver.o

bin/ui/LaserFinder.o : src/ui/LaserFinder.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/ui/LaserFinder.cpp -o bin/ui/LaserFinder.o

bin/ui/Calibrator.o : src/ui/Calibrator.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/ui/Calibrator.cpp -o bin/ui/Calibrator.o

bin/ui/CornerCalibrator.o : src/ui/CornerCalibrator.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/ui/CornerCalibrator.cpp -o bin/ui/CornerCalibrator.o

bin/ui/LaserManualCalibrator.o : src/ui/LaserManualCalibrator.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/ui/LaserManualCalibrator.cpp -o bin/ui/LaserManualCalibrator.o

bin/ui/LaserAutoCalibrator.o : src/ui/LaserAutoCalibrator.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/ui/LaserAutoCalibrator.cpp -o bin/ui/LaserAutoCalibrator.o

bin/parsing/CommandLineParser.o : src/parsing/CommandLineParser.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/parsing/CommandLineParser.cpp -o bin/parsing/CommandLineParser.o
	
bin/parsing/Lexer.o : src/parsing/Lexer.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/parsing/Lexer.cpp -o bin/parsing/Lexer.o


# dispatcher
bin/dispatcher/LaserDispatcher.o : 	src/dispatcher/LaserDispatcher.cpp \
					src/dispatcher/LaserDispatcher.h					
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/dispatcher/LaserDispatcher.cpp -o bin/dispatcher/LaserDispatcher.o

bin/dispatcher/LaserEvent.o : src/dispatcher/LaserEvent.cpp src/dispatcher/LaserEvent.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/dispatcher/LaserEvent.cpp -o bin/dispatcher/LaserEvent.o


# database
bin/database/TrackBase.o : src/database/TrackBase.cpp src/database/TrackBase.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/database/TrackBase.cpp -o bin/database/TrackBase.o

# calculator
bin/calculator/Calculator.o : src/calculator/Calculator.cpp src/calculator/Calculator.h \
			src/model/MappedRecognizer.h src/model/MappedRecognizer.impl.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/calculator/Calculator.cpp -o bin/calculator/Calculator.o

# Learner
bin/Learner.o : src/Learner.cpp src/Learner.h src/common.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/Learner.cpp -o bin/Learner.o

# CrissCross
bin/crisscross/CrissCross.o : src/crisscross/CrissCross.cpp src/crisscross/CrissCross.h src/common.h \
			src/model/MappedRecognizer.h src/model/MappedRecognizer.impl.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/crisscross/CrissCross.cpp -o bin/crisscross/CrissCross.o

bin/crisscross/CrissCrossListener.o : src/crisscross/CrissCrossListener.cpp \
					src/crisscross/CrissCrossListener.h src/common.h \
					src/model/MappedRecognizer.h src/model/MappedRecognizer.impl.h
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/crisscross/CrissCrossListener.cpp -o bin/crisscross/CrissCrossListener.o

###################################################
# TESTER
###################################################
TESTER_OBJS = 	bin/tester/tester.o\
		bin/io/HMMBuilder.o \
		bin/io/TrackLoader.o \
		bin/model/HMM.o \
		bin/model/Track.o \
		bin/model/Point.o \
		src/algs/Algorithms.cpp \
		src/algs/TrainAlgs.cpp

tester : bin/tester

bin/tester : $(TESTER_OBJS) $(HDRS)
	$(CXX) $(INCPATH) $(CXXFLAGS) $(TESTER_OBJS) $(LINKFLAGS) $(LIBS) -o bin/tester/tester

bin/tester/tester.o : src/tester/tester.cpp
	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/tester/tester.cpp -o bin/tester/tester.o

#bin/io/TrackLoader.o : src/io/TrackLoader.cpp
#	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/io/TrackLoader.cpp -o bin/io/TrackLoader.o

#bin/model/Track.o : src/model/Track.cpp
#	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/model/Track.cpp -o bin/model/Track.o

#bin/model/Point.o : src/model/Point.cpp
#	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/model/Point.cpp -o bin/model/Point.o

#bin/algs/Algorithms.o : src/algs/Algorithms.cpp
#	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/algs/Algorithms.cpp -o bin/algs/Algorithms.o

#bin/algs/TrainAlgs.o : src/algs/TrainAlgs.cpp
#	$(CXX) $(INCPATH) $(CXXFLAGS) -c src/algs/TrainAlgs.cpp -o bin/algs/TrainAlgs.o


clean:
	rm -f ./bin/lm $(OBJS)

objs:
	@echo $(OBJS)
	
run :
	./bin/lm

